package com.example.autopia.view.Fragments

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.autopia.R
import com.example.autopia.databinding.FragmentProfileBinding
import com.example.autopia.utilities.ImageManager
import com.example.autopia.utilities.PreferenceManager
import com.example.autopia.view.Activities.AboutAppActivity
import com.example.autopia.view.Activities.EditProfileActivity
import com.example.autopia.view.Activities.HelpActivity
import com.example.autopia.view.Activities.LikeNewsActivity
import com.example.autopia.view.Activities.LoginActivity
import com.example.autopia.view.Activities.MyCarsActivity
import com.example.autopia.view.Activities.ThemeActivity
import com.google.firebase.auth.FirebaseAuth


class ProfileFragment : Fragment() {
    private lateinit var binding: FragmentProfileBinding
    private val imageManager = ImageManager()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        binding = FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.apply {
            btSettings.setOnClickListener {//Переключение меню
                btProfile.setBackgroundResource(android.R.color.transparent)
                btSettings.setBackgroundResource(R.drawable.button_profile_menu)
                ProfileItems.visibility = View.GONE
                SettingsItems.visibility = View.VISIBLE
            }
            btProfile.setOnClickListener {//Переключение меню
                btSettings.setBackgroundResource(android.R.color.transparent)
                btProfile.setBackgroundResource(R.drawable.button_profile_menu)
                SettingsItems.visibility = View.GONE
                ProfileItems.visibility = View.VISIBLE
            }

            toMyCars.setOnClickListener {
                startActivity(Intent(requireContext(),MyCarsActivity::class.java))
            }

            toLikeNews.setOnClickListener {
                startActivity(Intent(requireContext(),LikeNewsActivity::class.java))
            }

            cvPersonalData.setOnClickListener {
                startActivity(Intent(requireContext(),EditProfileActivity::class.java))
            }

            cvTheme.setOnClickListener {
                startActivity(Intent(requireContext(),ThemeActivity::class.java))
            }

            cvLogout.setOnClickListener {//Выход из приложения
                FirebaseAuth.getInstance().signOut()
                val intent = Intent(requireContext(), LoginActivity::class.java)
                startActivity(intent)
                activity?.finish()
            }

            cvAboutApp.setOnClickListener {
                startActivity(Intent(requireContext(), AboutAppActivity::class.java))
            }

            cvHelp.setOnClickListener {
                startActivity(Intent(requireContext(), HelpActivity::class.java))
            }
        }
    }

    override fun onResume() {//Получение данных
        super.onResume()
        val userPreference = PreferenceManager(requireContext())
        binding.apply {
            userName.text = userPreference.getString("userName")
            if (userPreference.getString("userPhoto") != null&& userPreference.getString("userPhoto") != "null") {
                rivLogo.setImageBitmap(imageManager.byteArrayToBitmap(userPreference.getString("userPhoto")!!))
            }
        }
    }
}